home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / prgs / astronomy / RandomWalk.b < prev   
Encoding:
Text File  |  1996-08-28  |  5.1 KB  |  213 lines

  1. {*
  2. ** Random Walk
  3. ** ===========
  4. ** The idea here is to simulate the process by which a particle
  5. ** in the sun (or other star) eventually moves to the surface by 
  6. ** randomly colliding with neighbouring particles. The claim is
  7. ** that such a particle will _always_ reach the surface and escape.
  8. ** This program conducts an experiment to test this claim.
  9. **
  10. ** This program was suggested in a lecture given by Joss 
  11. ** Bland-Hawthorn at an astronomy seminar called "Colours 
  12. ** of the Stars" on May 10th and 11th 1997 at the University of Adelaide.
  13. **
  14. ** Author: David Benn
  15. **   Date: June 15 1997,
  16. **       July 20 1997 
  17. *}
  18.  
  19. Const RADCONV=57.295779
  20. Const iGenerate=1, iWalk=2, iClear=3, iSetStars=4, iSetRadius=5, iAbout=6, iQuit=7
  21. Const TRUE=-1&, FALSE=0&, having_fun=TRUE
  22. Const x_origin=320, y_origin=100 
  23. Const LEFT=0, UP=0, RIGHT=1, DOWN=1
  24. Const STAR_PARTICLE=1, MOVING_PARTICLE=2
  25.  
  26. {*
  27. ** Globals.
  28. *}
  29. Longint Stars, Radius
  30. Stars = 2000 : Radius = 75
  31.  
  32. {* 
  33. ** Subprogram declarations.
  34. *}
  35. Declare Sub Quit
  36. Declare Sub HandleMenu
  37. Declare Sub GenerateStar
  38. Declare Sub RandomWalk
  39. Declare Sub Longint ParticleCollision(Shortint x, Shortint y)
  40. Declare Sub Longint BeyondSurface(Shortint x, Shortint y)
  41.  
  42. {* 
  43. ** Main.
  44. *}
  45. Randomize Timer
  46.  
  47. '..Open window.
  48. Window 1,"Random Walk",(0,0)-(640,200)
  49.  
  50. '..Set up menus.
  51. Menu 1,0,1,"Project"
  52. Menu 1,iGenerate,1,"Generate",        "G"
  53. Menu 1,iWalk,1,"Walk",            "W"
  54. Menu 1,iClear,1,"Clear",        "C"
  55. Menu 1,iSetStars,1,"Set Stars...",    "S"
  56. Menu 1,iSetRadius,1,"SetRadius...",    "R"
  57. Menu 1,iAbout,1,"About...",        "A"
  58. Menu 1,iQuit,1,"Quit",            "Q"
  59.  
  60. '..Set up event handlers.
  61. On Window Call Quit : Window On
  62. On Break Call Quit : Break On
  63. On Menu Call HandleMenu : Menu On
  64.  
  65. While having_fun
  66.   Sleep
  67. Wend
  68.  
  69. End
  70.  
  71. {* 
  72. ** Subprogram definitions.
  73. *}
  74. Sub Quit
  75. {*
  76. ** Close window and exit program.
  77. *}
  78.   Window Close 1
  79.   Stop
  80. End Sub
  81.  
  82. Sub Longint GetPositiveInt(String prompt, String reqTitle, Longint default)
  83. {*
  84. ** Return an integer which is greater than zero
  85. ** via an input requester. If zero is returned
  86. ** from the requester, assume the cancel button
  87. ** has been clicked and return the default value.
  88. *}
  89. Longint n
  90.  
  91.   Repeat
  92.     n = InputBox(prompt, reqTitle, Mid$(Str$(default),2))
  93.   Until n >= 0 
  94.  
  95.   If n = 0 Then GetPositiveInt = default Else GetPositiveInt = n
  96. End Sub
  97.  
  98. Sub HandleMenu
  99. {*
  100. ** Handle menu choice.
  101. *}
  102. Shared Stars, Radius
  103. Shortint theItem
  104.  
  105.   If Menu(0) = 1 Then
  106.     theItem = Menu(1)
  107.     Case
  108.       theItem = iGenerate : GenerateStar
  109.       theItem = iWalk     : RandomWalk
  110.       theItem = iClear    : Cls
  111.       theItem = iSetStars : Stars = GetPositiveInt("Enter number of stars:","Set Stars",Stars)
  112.       theItem = iSetRadius: Radius = GetPositiveInt("Enter radius:","Set Radius",Radius)
  113.       theItem = iAbout    : MsgBox "Random Walk - David Benn 1997","Ok"
  114.       theItem = iQuit     : Quit
  115.     End Case
  116.   End If
  117. End Sub
  118.  
  119. Sub GenerateStar
  120. {*
  121. ** Generate a random circular area of stars
  122. ** and fill x,y arrays with points.
  123. *}
  124. Shared Stars, Radius
  125. Single theta
  126. Shortint i,x,y
  127.  
  128.   For i=1 to Stars
  129.     theta = (Rnd*360)/RADCONV
  130.     x = x_origin + Rnd*Radius*Cos(theta)
  131.     y = y_origin + Rnd*Radius*Sin(theta)
  132.     Pset(x,y),STAR_PARTICLE
  133.   Next i
  134. End Sub
  135.  
  136. Sub RandomWalk
  137. {*
  138. ** Choose a particle at random and let it move
  139. ** at random. The process stops when the particle
  140. ** reaches the star's surface.
  141. *}
  142. Shared Radius
  143. Single theta
  144. Shortint x,y
  145. Shortint x_dir,y_dir
  146.  
  147.   '..Create a particle to move.
  148.   theta = (Rnd*360)/RADCONV
  149.   x = x_origin + Rnd*Radius*Cos(theta)
  150.   y = y_origin + Rnd*Radius*Sin(theta)
  151.   Pset(x,y),MOVING_PARTICLE
  152.  
  153.   '..Random walk.
  154.   x_dir = Rnd*2 : y_dir = Rnd*2      {* random x and y direction *}
  155.   Repeat
  156.     '..Prepare to move particle by erasing it. First make sure
  157.     '..that we're not erasing another particle after a collision.
  158.     If Point(x,y) = MOVING_PARTICLE Then Pset(x,y),0
  159.  
  160.     '..Move particle. 
  161.     If x_dir = LEFT Then --x Else ++x    
  162.     If y_dir = UP Then --y Else ++y 
  163.  
  164.     {* 
  165.     ** If the particle collides with another,
  166.     ** randomly change its direction, otherwise
  167.     ** just plot it. Don't want to plot it if
  168.     ** there's been a collision otherwise we'll
  169.     ** lose a particle. Actually, may _want_ to
  170.     ** do so sometimes since some collisions
  171.     ** will result in fusion! See commented line
  172.     ** in first case below.
  173.     *}
  174.     If ParticleCollision(x,y) Then
  175.     x_dir = Rnd*2 : y_dir = Rnd*2 
  176.           {* If Rnd*5 = 4 Then Pset(x(p),y(p)),MOVING_PARTICLE -- fusion? -- *} 
  177.     Else
  178.     Pset(x,y),MOVING_PARTICLE
  179.     End If
  180.     
  181.     Sleep For 0.1
  182.   Until BeyondSurface(x,y)
  183.   
  184.   '..Escaped!
  185.   Say Translate$("The particle has escaped.")
  186. End Sub
  187.  
  188. Sub Longint ParticleCollision(Shortint x, Shortint y)
  189. {*
  190. ** Return T or F indicating whether particle has collided
  191. ** with another.
  192. *}
  193.   If Point(x,y) = STAR_PARTICLE Then 
  194.     ParticleCollision = TRUE 
  195.   Else
  196.       ParticleCollision = FALSE 
  197.   End If
  198. End Sub
  199.  
  200. Sub Longint BeyondSurface(Shortint x, Shortint y)
  201. {*
  202. ** Return T or F indicating whether particle has reached
  203. ** beyond the star's surface.
  204. *}
  205. Shared Radius
  206.   If x < x_origin - Radius Or x > x_origin + Radius Or ~
  207.      y < y_origin - Radius Or y > y_origin + Radius Then
  208.     BeyondSurface = TRUE
  209.   Else
  210.     BeyondSurface = FALSE
  211.   End If
  212. End Sub
  213.